// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import GameData_red_blue_man from "./GameData_red_blue_man";
import Music, {musicName} from "../PublicScript/Music";
import ControlScript from "../PublicScript/ControlScript";
import GiftMistake from "./GiftMistake";
import Money from "./Money";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GiftFrame extends cc.Component {

    @property(cc.ProgressBar)
    progressBar: cc.ProgressBar = null;

    @property(sp.Skeleton)
    red: sp.Skeleton = null;

    @property(sp.Skeleton)
    blue: sp.Skeleton = null;

    @property(cc.Node)
    buttonState1: cc.Node = null;

    @property(cc.Node)
    buttonState2: cc.Node = null;

    @property({displayName: "对应的位置下标"})
    index: number = 0;

    @property({displayName: "需要观看的次数"})
    videoCount: number = 0;

    @property(cc.Label)
    countLabel: cc.Label = null;

    @property(cc.Node)
    giftParent: cc.Node = null;

    @property()
    nodeIndex = 0;

    isPush = false;

    onLoad () {
        this.init();
    }

    start () {

    }

    init(){
        this.red.setSkin("red" + this.index);
        this.blue.setSkin("blue" + this.index);
        this.show(true);
    }

    onClick(){
        Music.GamePlayEffect(musicName.click);
        if(GameData_red_blue_man.giftCount[this.index] >= this.videoCount){
            return;
        }
        let self = this;
        GameData_red_blue_man.showVideo({callback: ()=>{
                GameData_red_blue_man.giftCount[this.index] ++;
                Money.addMoney(200);
                if(GameData_red_blue_man.giftCount[this.index] >= this.videoCount){
                    ControlScript.createToast("获得200金币！成功解锁对应皮肤！")
                }else{
                    ControlScript.createToast("获得200金币！累计次数增加1！")
                }
                GameData_red_blue_man.saveGiftCount();
                self.show();
            }})
    }

    show(init?){
        if(GameData_red_blue_man.giftCount[this.index]){
            this.progressBar.progress = GameData_red_blue_man.giftCount[this.index] / this.videoCount;
        }else{
            GameData_red_blue_man.giftCount[this.index] = 0;
            this.progressBar.progress = 0;
        }

        if(GameData_red_blue_man.giftCount[this.index] >= this.videoCount || GameData_red_blue_man.skinData[this.index - 1].unLock == 1){
            this.buttonState1.active = false;
            this.buttonState2.active = true;
            this.progressBar.progress = 1;
            GameData_red_blue_man.skinData[this.index - 1].unLock = 1;
            GameData_red_blue_man.giftCount[this.index] = this.videoCount;
            GameData_red_blue_man.saveSkinData();
            console.log("已经解锁了！！！");
            let index = GameData_red_blue_man.giftNodeList.indexOf(this.nodeIndex);
            if(index > -1){
                GameData_red_blue_man.giftNodeList.splice(index, 1);
                console.log(GameData_red_blue_man.giftNodeList);
                this.giftParent.getComponent(GiftMistake).showHand();
            }
        }else{
            if(!this.isPush){
                this.isPush = true;
                GameData_red_blue_man.giftNodeList.push(this.nodeIndex);
                console.log(GameData_red_blue_man.giftNodeList);
            }
        }

        if(init){
            GameData_red_blue_man.giftNodeCount ++;
            console.log(GameData_red_blue_man.giftNodeCount);
            if(GameData_red_blue_man.giftNodeCount >= 5){
                this.giftParent.getComponent(GiftMistake).showHand();
            }
        }

        this.countLabel.string = GameData_red_blue_man.giftCount[this.index] + ";" + this.videoCount;
    }

    protected onDestroy(): void {
        GameData_red_blue_man.giftNodeList = [];
        GameData_red_blue_man.giftNodeCount = 0;
    }

    // update (dt) {}
}
